package com.zhuangfei.timetable.listener;


import com.zhuangfei.ResourceTable;
import com.zhuangfei.timetable.model.ScheduleSupport;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import java.util.Calendar;
import java.util.List;

/**
 * 日期栏的构建过程.
 */
public class OnDateBuildAapter implements ISchedule.OnDateBuildListener {

    private static final String TAG = "OnDateBuildAapter";

    /**
     * 第一个：月份，之后7个表示周一至周日
     */
    protected Text[] textViews = new Text[8];
    protected DirectionalLayout[] layouts = new DirectionalLayout[8];

    protected int background = Color.getIntColor("#F4F8F8");
    protected float alpha = 1;

    protected String[] dateArray;
    protected List<String> weekDates;
    protected DirectionalLayout layout;

    public OnDateBuildAapter setBackground(int background) {
        this.background = background;
        return this;
    }

    @Override
    public void onInit(DirectionalLayout layout, float alpha) {
        this.alpha = alpha;
        this.layout = layout;
        /**
         * 星期设置
         */
        dateArray = getStringArray();
        weekDates = ScheduleSupport.getWeekDate();
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(244,248,248));
        element.setAlpha((int) alpha);
        if (layout != null) {
            layout.setBackground(element);
        }
    }

    @Override
    public Component[] getDateViews(LayoutScatter mInflate, float monthWidth, float perWidth, int height) {
        Component[] views = new Component[8];
        views[0] = onBuildMonthLayout(mInflate, (int) monthWidth, height);
        for (int i = 1; i < 8; i++) {
            views[i] = onBuildDayLayout(mInflate, i, (int) perWidth, height);
        }
        return views;
    }


    @Override
    public void onHighLight() {
        initDateBackground();

        /**
         * 获取周几，1->7
         */
        Calendar now = Calendar.getInstance();
        /**
         * 一周第一天是否为星期天
         */
        boolean isFirstSunday = now.getFirstDayOfWeek() == Calendar.SUNDAY;
        int weekDay = now.get(Calendar.DAY_OF_WEEK);
        /**
         * 若一周第一天为星期天，则-1
         */
        if (isFirstSunday) {
            weekDay = weekDay - 1;
            if (weekDay == 0) {
                weekDay = 7;
            }
        }

        activeDateBackground(weekDay);
    }


    @Override
    public void onUpdateDate(int curWeek, int targetWeek) {
        if (textViews == null || textViews.length < 8) {
            return;
        }
        weekDates = ScheduleSupport.getDateStringFromWeek(curWeek, targetWeek);
        int month = Integer.parseInt(weekDates.get(0));
        textViews[0].setText(month + "\n月");
        for (int i = 1; i < 8; i++) {
            if (textViews[i] != null) {
                textViews[i].setText(weekDates.get(i) + "日");
            }
        }
    }

    /**
     * 构建月份，也就是日期栏的第一格.<br/>
     * 宽度、高度均为px
     *
     * @param mInflate
     * @param width    宽度
     * @param height   默认高度
     * @return Component
     */
    protected Component onBuildMonthLayout(LayoutScatter mInflate, int width, int height) {
        Component first = mInflate.parse(ResourceTable.Layout_item_dateview_first, null, true);
        /**
         * 月份设置
         */
        textViews[0] = (Text) first.findComponentById(ResourceTable.Id_id_week_month);
        layouts[0] = null;

        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(width, height);

        int month = Integer.parseInt(weekDates.get(0));
        first.setLayoutConfig(lp);
        textViews[0].setText(month + "\n月");
        return first;
    }

    protected Component onBuildDayLayout(LayoutScatter mInflate, int pos, int width, int height) {
        Component view = mInflate.parse(ResourceTable.Layout_item_dateview, null, true);
        Text dayTextView = (Text) view.findComponentById(ResourceTable.Id_id_week_day);
        dayTextView.setText(dateArray[pos]);

        textViews[pos] = (Text) view.findComponentById(ResourceTable.Id_id_week_date);
        layouts[pos] = (DirectionalLayout) view.findComponentById(ResourceTable.Id_id_week_layout);

        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(width, height);
        layouts[pos].setLayoutConfig(lp);
        textViews[pos].setText(weekDates.get(pos) + "日");

        return view;
    }

    /**
     * 返回一个长度为8的数组，第0个位置为null
     *
     * @return String[]
     */
    public String[] getStringArray() {
        return new String[]{null, "周一", "周二", "周三", "周四", "周五", "周六", "周日"};
    }

    protected void initDateBackground() {
        for (int i = 1; i < 8; i++) {
            int value = Color.TRANSPARENT.getValue();
            if (layouts[i] != null) {
                ShapeElement shapeElement = new ShapeElement();
                shapeElement.setRgbColor(new RgbColor(value));
                layouts[i].setBackground(shapeElement);
            }
        }
    }

    protected void activeDateBackground(int weekDay) {
        if (layouts.length > weekDay && layouts[weekDay] != null) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(191,246,244));
            shapeElement.setAlpha((int) alpha);
            layouts[weekDay].setBackground(shapeElement);
        }
    }
}
